<?php
namespace App\Service\Admin\Mall;

class GoodsService extends BaseService
{
    //商城商品

    //列表
    public static function index()
    {
        $db = self::dbConn(self::$dbMallGoods)->where('del', 0);
        $total = $db->count();
        $res = $db->orderBy('id', 'desc')->get();
        if (count($res)) {
            foreach ($res as &$v) {
                $cateOne = self::dbConn(self::$dbCate)->where('id', $v->cate_id)->first();
                $shopOne = self::dbConn(self::$dbMallShop)->where('id', $v->shop_id)->first();
                $imgOnes = self::dbConn(self::$dbMallGimg)
                    ->where('del', 0)
                    ->where('gid', $v->id)
                    ->orderBy('id', 'asc')
                    ->get();
                $v->cate_name = $cateOne ? $cateOne->title : '';
                $v->shop_name = $shopOne ? $shopOne->shop_name : '';
                $v->img01 = isset($imgOnes[0])&&file_exists($imgOnes[0]->img) ? $imgOnes[0]->img : '';
                $v->img02 = isset($imgOnes[1])&&file_exists($imgOnes[1]->img) ? $imgOnes[1]->img : '';
                $v->img03 = isset($imgOnes[2])&&file_exists($imgOnes[2]->img) ? $imgOnes[2]->img : '';
                $v->img04 = isset($imgOnes[3])&&file_exists($imgOnes[3]->img) ? $imgOnes[3]->img : '';
            }
        }
        return self::toback(0, '', $res, $total);
    }

    //选项
    public static function option()
    {
        $ary['cate']  = self::dbConn(self::$dbCate)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('type', 1)
            ->get();
        $ary['shop']  = self::dbConn(self::$dbMallShop)
            ->select('id', 'shop_name as title')
            ->where('status', 0)
            ->get();
        return $ary;
    }

    /**
     * 新增
     * @param array $pas
     */
    public static function add($pas)
    {
        $pas['gcode'] = toCode();
        $pas['uptime'] = $pas['addtime'] = toDate();
        $res = self::dbConn(self::$dbMallGoods)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback(0, '', $res);
    }

    /**
     * 修改
     * @param int $id
     * @param array $pas
     */
    public static function modify($id, $pas)
    {
        $pas['uptime'] = toDate();
        $up = self::dbConn(self::$dbMallGoods)
            ->where('id', $id)
            ->update($pas);
        if (!$up) {
            return self::toback(-1, 'error');
        }
        return self::toback(0, '', $id);
    }

    /**
     * 图片更新
     * @param int $gid
     * @param string $img
     */
    public static function addImgs($gid, $img)
    {
        $res = self::dbConn(self::$dbMallGimg)->where('del', 0)
            ->where('img', $img)
            ->first();
        if ($res) return;
        self::dbConn(self::$dbMallGimg)->insertGetId([
            'type' => 1, 'gid' => $gid,
            'img' => $img, 'addtime' => toDate(),
        ]);
        return;
    }

    /**
     * 删除
     * @param int $id 场景ID
     * return array
     */
    public static function del($id)
    {
        $pas = [
            'del' => 1,
        ];
        $res = self::dbConn(self::$dbMallGoods)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}